{
  "properties" : { },
  "id" : "e836a212f3ca4d22812f075110b75497",
  "script" : null,
  "groupId" : "08c2af4fd56543e690b14515d83d66ae",
  "name" : "mongoDb查询相关",
  "createTime" : null,
  "updateTime" : 1648126801789,
  "lock" : null,
  "createBy" : null,
  "updateBy" : "admin",
  "path" : "mongoDbSimpleTest",
  "method" : "POST",
  "parameters" : [ ],
  "options" : [ ],
  "requestBody" : "{\r\n    \"data\": {\r\n        \"currentPage\": 1,\r\n        \"pageSize\": 10,\r\n        \"sort\": {\r\n            \"updateTime\": -1,\r\n            \"eventNucleicAcid.collectTime\" : -1\r\n        },\r\n        \"match\": {\r\n            \"dangerType\":\"1\"\r\n        },\r\n        \"like\":{\r\n\r\n        },\r\n        \"date\":{\r\n\r\n        },\r\n        \"fields\":[\"name\",\"updateTime\"]\r\n    }\r\n}",
  "headers" : [ ],
  "paths" : [ ],
  "responseBody" : "{\n    \"code\": 200,\n    \"message\": \"success\",\n    \"data\": {\n        \"content\": [{\n            \"_id\": \"623b06c32534627939a6b9d7\",\n            \"name\": \"刘安\",\n            \"updateTime\": \"2022-03-23 19:38:43\"\n        }, {\n            \"_id\": \"623b0ceb2534627939a6b9d9\",\n            \"name\": \"赵晓斐\",\n            \"updateTime\": \"2022-03-23 20:04:58\"\n        }, {\n            \"_id\": \"623b0e162534627939a6b9dc\",\n            \"name\": \"勿动勿删\",\n            \"updateTime\": \"2022-03-24 15:45:51\"\n        }, {\n            \"_id\": \"1483645180782338049\",\n            \"name\": \"测试排查2\",\n            \"updateTime\": \"2022-03-23 20:37:47\"\n        }, {\n            \"_id\": \"623b1a982534627939a6bae7\",\n            \"name\": \"eqw\",\n            \"updateTime\": \"2022-03-23 21:03:20\"\n        }, {\n            \"_id\": \"623b1ac62534627939a6baea\",\n            \"name\": \"eqw\",\n            \"updateTime\": \"2022-03-23 21:04:06\"\n        }, {\n            \"_id\": \"623b1d432534627939a6baed\",\n            \"name\": \"赵晓斐\",\n            \"updateTime\": \"2022-03-23 21:14:43\"\n        }, {\n            \"_id\": \"623bd99f2534627939a6bb2e\",\n            \"name\": \"罗病例\",\n            \"updateTime\": \"2022-03-24 10:38:23\"\n        }, {\n            \"_id\": \"623bda402534627939a6bb30\",\n            \"name\": \"赵龙\",\n            \"updateTime\": \"2022-03-24 10:41:04\"\n        }, {\n            \"_id\": \"623bddf62534627939a6bb3c\",\n            \"name\": \"姓名\",\n            \"updateTime\": \"2022-03-24 10:56:54\"\n        }, {\n            \"_id\": \"623bdee82534627939a6bb3f\",\n            \"name\": \"病例刘\",\n            \"updateTime\": \"2022-03-24 11:00:56\"\n        }, {\n            \"_id\": \"623c09ae2534627939a6bb62\",\n            \"name\": \"老大妈\",\n            \"updateTime\": \"2022-03-24 14:03:26\"\n        }, {\n            \"_id\": \"623c0cd62534627939a6bb69\",\n            \"name\": \"eqwe\",\n            \"updateTime\": \"2022-03-24 14:16:54\"\n        }, {\n            \"_id\": \"623c1c3b2534627939a6bb7b\",\n            \"name\": \"迁移354test\",\n            \"updateTime\": \"2022-03-24 15:27:13\"\n        }, {\n            \"_id\": \"623c4fd42534627939a6bbad\",\n            \"name\": \"让人人\",\n            \"updateTime\": \"2022-03-24 19:02:44\"\n        }, {\n            \"_id\": \"623c50af2534627939a6bbb0\",\n            \"name\": \"赵晓斐\",\n            \"updateTime\": \"2022-03-24 19:20:33\"\n        }],\n        \"pageable\": {\n            \"sort\": {\n                \"sorted\": false,\n                \"unsorted\": true,\n                \"empty\": true\n            },\n            \"offset\": 0,\n            \"pageSize\": 10,\n            \"pageNumber\": 0,\n            \"unpaged\": false,\n            \"paged\": true\n        },\n        \"totalElements\": 16,\n        \"totalPages\": 2,\n        \"last\": false,\n        \"size\": 10,\n        \"number\": 0,\n        \"sort\": {\n            \"sorted\": false,\n            \"unsorted\": true,\n            \"empty\": true\n        },\n        \"numberOfElements\": 16,\n        \"first\": true,\n        \"empty\": false\n    },\n    \"timestamp\": 1648126563219,\n    \"executeTime\": 288\n}",
  "description" : null,
  "requestBodyDefinition" : {
    "name" : "",
    "value" : "",
    "description" : "",
    "required" : false,
    "dataType" : "Object",
    "type" : null,
    "defaultValue" : null,
    "validateType" : "",
    "error" : "",
    "expression" : "",
    "children" : [ {
      "name" : "data",
      "value" : "",
      "description" : "",
      "required" : false,
      "dataType" : "Object",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ {
        "name" : "currentPage",
        "value" : "1",
        "description" : "",
        "required" : false,
        "dataType" : "Integer",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "pageSize",
        "value" : "10",
        "description" : "",
        "required" : false,
        "dataType" : "Integer",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "sort",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Object",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ {
          "name" : "updateTime",
          "value" : "-1",
          "description" : "",
          "required" : false,
          "dataType" : "Integer",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        }, {
          "name" : "eventNucleicAcid.collectTime",
          "value" : "-1",
          "description" : "",
          "required" : false,
          "dataType" : "Integer",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        } ]
      }, {
        "name" : "match",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Object",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ {
          "name" : "dangerType",
          "value" : "1",
          "description" : "",
          "required" : false,
          "dataType" : "String",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        } ]
      }, {
        "name" : "like",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Object",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "date",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Object",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "fields",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Array",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ {
          "name" : "",
          "value" : "name",
          "description" : "",
          "required" : false,
          "dataType" : "String",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        } ]
      } ]
    } ]
  },
  "responseBodyDefinition" : {
    "name" : "",
    "value" : "",
    "description" : "",
    "required" : false,
    "dataType" : "Object",
    "type" : null,
    "defaultValue" : null,
    "validateType" : "",
    "error" : "",
    "expression" : "",
    "children" : [ {
      "name" : "code",
      "value" : "200",
      "description" : "",
      "required" : false,
      "dataType" : "Integer",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    }, {
      "name" : "message",
      "value" : "success",
      "description" : "",
      "required" : false,
      "dataType" : "String",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    }, {
      "name" : "data",
      "value" : "",
      "description" : "",
      "required" : false,
      "dataType" : "Object",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ {
        "name" : "content",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Array",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ {
          "name" : "",
          "value" : "",
          "description" : "",
          "required" : false,
          "dataType" : "Object",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ {
            "name" : "_id",
            "value" : "623b06c32534627939a6b9d7",
            "description" : "",
            "required" : false,
            "dataType" : "Object",
            "type" : null,
            "defaultValue" : null,
            "validateType" : "",
            "error" : "",
            "expression" : "",
            "children" : [ ]
          }, {
            "name" : "name",
            "value" : "刘安",
            "description" : "",
            "required" : false,
            "dataType" : "String",
            "type" : null,
            "defaultValue" : null,
            "validateType" : "",
            "error" : "",
            "expression" : "",
            "children" : [ ]
          }, {
            "name" : "updateTime",
            "value" : "2022-03-23 19:38:43",
            "description" : "",
            "required" : false,
            "dataType" : "String",
            "type" : null,
            "defaultValue" : null,
            "validateType" : "",
            "error" : "",
            "expression" : "",
            "children" : [ ]
          } ]
        } ]
      }, {
        "name" : "pageable",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Object",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ {
          "name" : "sort",
          "value" : "",
          "description" : "",
          "required" : false,
          "dataType" : "Object",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ {
            "name" : "sorted",
            "value" : "false",
            "description" : "",
            "required" : false,
            "dataType" : "Boolean",
            "type" : null,
            "defaultValue" : null,
            "validateType" : "",
            "error" : "",
            "expression" : "",
            "children" : [ ]
          }, {
            "name" : "unsorted",
            "value" : "true",
            "description" : "",
            "required" : false,
            "dataType" : "Boolean",
            "type" : null,
            "defaultValue" : null,
            "validateType" : "",
            "error" : "",
            "expression" : "",
            "children" : [ ]
          }, {
            "name" : "empty",
            "value" : "true",
            "description" : "",
            "required" : false,
            "dataType" : "Boolean",
            "type" : null,
            "defaultValue" : null,
            "validateType" : "",
            "error" : "",
            "expression" : "",
            "children" : [ ]
          } ]
        }, {
          "name" : "offset",
          "value" : "0",
          "description" : "",
          "required" : false,
          "dataType" : "Integer",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        }, {
          "name" : "pageSize",
          "value" : "10",
          "description" : "",
          "required" : false,
          "dataType" : "Integer",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        }, {
          "name" : "pageNumber",
          "value" : "0",
          "description" : "",
          "required" : false,
          "dataType" : "Integer",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        }, {
          "name" : "unpaged",
          "value" : "false",
          "description" : "",
          "required" : false,
          "dataType" : "Boolean",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        }, {
          "name" : "paged",
          "value" : "true",
          "description" : "",
          "required" : false,
          "dataType" : "Boolean",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        } ]
      }, {
        "name" : "totalElements",
        "value" : "16",
        "description" : "",
        "required" : false,
        "dataType" : "Integer",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "totalPages",
        "value" : "2",
        "description" : "",
        "required" : false,
        "dataType" : "Integer",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "last",
        "value" : "false",
        "description" : "",
        "required" : false,
        "dataType" : "Boolean",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "size",
        "value" : "10",
        "description" : "",
        "required" : false,
        "dataType" : "Integer",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "number",
        "value" : "0",
        "description" : "",
        "required" : false,
        "dataType" : "Integer",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "sort",
        "value" : "",
        "description" : "",
        "required" : false,
        "dataType" : "Object",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ {
          "name" : "sorted",
          "value" : "false",
          "description" : "",
          "required" : false,
          "dataType" : "Boolean",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        }, {
          "name" : "unsorted",
          "value" : "true",
          "description" : "",
          "required" : false,
          "dataType" : "Boolean",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        }, {
          "name" : "empty",
          "value" : "true",
          "description" : "",
          "required" : false,
          "dataType" : "Boolean",
          "type" : null,
          "defaultValue" : null,
          "validateType" : "",
          "error" : "",
          "expression" : "",
          "children" : [ ]
        } ]
      }, {
        "name" : "numberOfElements",
        "value" : "16",
        "description" : "",
        "required" : false,
        "dataType" : "Integer",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "first",
        "value" : "true",
        "description" : "",
        "required" : false,
        "dataType" : "Boolean",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "empty",
        "value" : "false",
        "description" : "",
        "required" : false,
        "dataType" : "Boolean",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      } ]
    }, {
      "name" : "timestamp",
      "value" : "1648126563219",
      "description" : "",
      "required" : false,
      "dataType" : "Long",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    }, {
      "name" : "executeTime",
      "value" : "288",
      "description" : "",
      "required" : false,
      "dataType" : "Integer",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    } ]
  }
}
================================
import log;//引入日志模块
import mongoTemplete;//引入自定义的mongoTemplete模块
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.data.domain.PageImpl
import org.apache.commons.collections.CollectionUtils;


    var tableName = "yqfk_person_info";
//取参
    var data = body.data;
    log.info("接收参数:"+data);
//处理参数 默认按更新时间倒叙
    // if (!data.sort){
    //     data.sort = {
    //         "updateTime":-1
    //     }
    // }
//处理参数 分页 默认第一页每页10条
    // if (!data.currentPage && !data.pageSize){
    //     data.currentPage = 1;
    //     data.pageSize = 10
    // }
//处理参数 新增一个查询条件 以下两种方式效果相同 类似于js处理json对象
    // data.match["delFlag"] = 0;
    // data.match.aa = 2;
//使用自定义函数组装查询对象 criteriaBuilder -- 自定义函数--可以直接使用
    var criteria = criteriaBuilder(data);
    // log.info(criteria.toString());
//自己组装查询对象
    // var criteria = Criteria.where("checkedFlag").is(1).and("delFlag").is(0).and("eventNucleicAcid.delFlag").is(0)
    // var criteria2 = Criteria.where("delFlag").is(0).and("eventNucleicAcid.delFlag").is(0);
    Query query = new Query(criteria);
//定义查询返回字段
    if (CollectionUtils.isNotEmpty(data.fields)){
        for (item in data.fields) {
            query.fields().include(item)
        }
    }
//查询总数 
    //查询对象，class对象，表名 
    var countQuery = query.limit(-1).skip(-1);//排除分页的影响
    var count = mongoTemplete.count(countQuery, null, tableName);
    log.info(count+"");
//排序
    // var sortOrder_a = Sort.Order.asc("eventNucleicAcid.collectTime");
    // var sort_a = Sort.by(sortOrder_a);
    // query.with(sort_a);
//多字段排序
    // var sort_b = Sort.by(sortOrder_a,sortOrder_a);
    // query.with(sort_b);
//需要由前端控制排序的时候---前端传入查询条件
    // var orderList = [];
    // orderList.add(sortOrder_a);//加入代码默认的排序
    // data.sort.forEach((k, v) -> {
    //     v == 1 ? orderList.add(Sort.Order.asc(k)) : orderList.add(Sort.Order.desc(k));
    // });
    // var sort_c = Sort.by(orderList);
    // query.with(sort_c);
//简单查询、
   //查询数据  List<Object> items 
    var items = mongoTemplete.find(query, tableName);
    if (CollectionUtils.isNotEmpty(items)) {
        items.forEach(o -> {
            o["_id"] = o["_id"].toString()
        });
    }
//简单分页查询
    //拼装分页对象
    Pageable pageable = PageRequest.of(data.currentPage - 1, data.pageSize);
    query.with(pageable);
     //得到分页对象
    var page = PageableExecutionUtils.getPage(items,pageable,() -> count);
    //-- 未测试 https://www.cnblogs.com/jycboy/archive/2018/04/28/8969035.html 去掉count减少分页查询的查询时间
    // var page = PageableExecutionUtils.getPage(items,pageable,() -> 0L);
    var a = page.totalElements;
    var b = page.totalPages;
//聚合查询
return page;